home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / csim / source.lha / source / C++SIM / Process.cc < prev    next >
C/C++ Source or Header  |  1993-06-14  |  4KB  |  203 lines

  1. /*
  2.  * Copyright (C) 1993
  3.  *
  4.  * Department of Computing Science,
  5.  * The University,
  6.  * Newcastle upon Tyne,
  7.  * UK.
  8.  */
  9.  
  10. #include <iostream.h>
  11. #include <stdlib.h>
  12.  
  13. #ifndef COMMON_H_
  14. #include "common.h"
  15. #endif
  16.  
  17. #ifndef PROCESS_H_
  18. #include "Process.h"
  19. #endif
  20.  
  21. #ifndef PROCESSITERATOR_H_
  22. #include "ProcessIterator.h"
  23. #endif
  24.  
  25. #ifndef PROCESSLIST_H_
  26. #include "ProcessList.h"
  27. #endif
  28.  
  29. #ifndef PROCESSCONS_H_
  30. #include "ProcessCons.h"
  31. #endif
  32.  
  33. static ProcessList ReadyQueue;
  34. static Process *Current = 0;
  35.  
  36. //
  37. // create scheduler task at MINPRIO
  38. //
  39.  
  40. Scheduler::Scheduler () : Thread_Type(MINPRIO) {}
  41.  
  42. Scheduler::~Scheduler () {}
  43.  
  44. void Scheduler::Body ()
  45. {
  46.     for (;;)
  47.     {
  48.     Current = ReadyQueue.Remove();
  49.     if (Current == 0)    // all done
  50.     {
  51. #ifdef DEBUG
  52.         cerr << "Scheduler queue is empty.\n";
  53. #endif
  54.         exit(0);
  55.     }
  56.  
  57.     if (Current->evtime() < 0)
  58.         cerr << "Scheduler Error: Process WakeupTime Invalid.\n";
  59.     else
  60.         SimulatedTime = Current->evtime();
  61.  
  62. #ifdef DEBUG
  63.     cerr << "Simulated time is now " << SimulatedTime << "\n";
  64. #endif
  65.  
  66.     Current->Resume();
  67.     }
  68. }
  69.  
  70.  
  71. //
  72. // Class Process
  73. //
  74.  
  75. const int Process::Never=-1; // Process will never awaken.
  76.  
  77. Process::~Process () {}
  78.  
  79. double Process::CurrentTime () { return SimulatedTime; }
  80.  
  81. void Process::ActivateBefore (Process &p)
  82. {
  83.     // No op if already scheduled
  84.     if (idle()) return;
  85.  
  86.     if (ReadyQueue.InsertBefore(*this, p))
  87.     wakeuptime = p.wakeuptime;
  88.     else
  89.     cerr << "ActivateBefore failed because 'before' process is not scheduled" << endl;
  90. }
  91.  
  92. void Process::ActivateAfter (Process &p)
  93. {
  94.     // No op if already scheduled
  95.     if (idle()) return;
  96.  
  97.     if (ReadyQueue.InsertAfter(*this, p))
  98.     wakeuptime = p.wakeuptime;
  99.     else
  100.     cerr << "ActivateAfter failed because 'after' process is not scheduled" << endl;
  101. }
  102.  
  103. void Process::ActivateAt (double AtTime, boolean prior)
  104. {
  105.     // No op if already scheduled
  106.     if (idle()) return;
  107.  
  108.     wakeuptime = AtTime;
  109.     ReadyQueue.Insert(*this, prior);
  110. }
  111.  
  112. void Process::ActivateDelay (double Delay, boolean prior)
  113. {
  114.     // No op if already scheduled
  115.     if (idle()) return;
  116.  
  117.     wakeuptime = SimulatedTime + Delay;
  118.     ReadyQueue.Insert(*this, prior);
  119. }
  120.  
  121.  
  122. // Similarly, there are four ways to reactivate
  123. // Note that if a process is already scheduled, the reactivate
  124. // will simply re-schedule the process.
  125.  
  126. void Process::ReActivateBefore (Process &p)
  127. {
  128.     Cancel();
  129.     ActivateBefore(p);
  130. }
  131.  
  132. void Process::ReActivateAfter  (Process &p)
  133. {
  134.     Cancel();
  135.     ActivateAfter(p);
  136. }
  137.  
  138. void Process::ReActivateAt (double AtTime, boolean prior)
  139. {
  140.     Cancel();
  141.     ActivateAt(AtTime, prior);
  142. }
  143.  
  144. void Process::ReActivateDelay (double Delay, boolean prior)
  145. {
  146.     Cancel();
  147.     ActivateDelay(Delay, prior);
  148. }
  149.  
  150. void Process::ReActivate ()
  151. {
  152.     if (idle())
  153.     ReadyQueue.Remove(this);
  154.     set_evtime(CurrentTime());
  155.     Resume();
  156. }
  157.  
  158. // cancels next burst of activity, process becomes idle
  159. void Process::Cancel ()
  160. {
  161.     if (idle())
  162.     ReadyQueue.Remove(this);
  163.     else
  164.     Suspend();
  165.  
  166.     wakeuptime = Process::Never;
  167. }
  168.  
  169. boolean Process::idle ()
  170. {
  171.     if (wakeuptime == CurrentTime())
  172.     return false;
  173.     else
  174.     return true;
  175. }
  176.  
  177. Process::Process() : Thread_Type()
  178. {
  179.     wakeuptime = CurrentTime();
  180. }
  181.  
  182. // suspend current process for simulated time t
  183. void Process::Hold (double t) 
  184. {
  185.     ActivateDelay(t);
  186.     Suspend();
  187. }
  188.  
  189. // suspend current process indefinitely (i.e., make idle)
  190. void Process::Passivate ()
  191. {
  192.     wakeuptime = Process::Never;
  193.     this->Terminated = true;
  194.     Suspend();
  195. }
  196.  
  197.  
  198. #ifdef NO_INLINES
  199. #  define PROCESS_CC_
  200. #  include "Process.n"
  201. #  undef PROCESS_CC_
  202. #endif
  203.